package com.mm.cloud.lease.core;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;

/**
 * @author mr
 * @since 2025-02-21
 */
public class CodeGenerator {

    private static final String mm_lease = "jdbc:mysql://127.0.0.1:3306/mm_lease?useSSL=false";

    private static final String username = "root";

    private static final String password = "root";

    public static void main(String[] args) {
        FastAutoGenerator.create(mm_lease, username, password)
                .globalConfig(builder -> {
                    builder.author("mr") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            .outputDir(Paths.get(System.getProperty("user.dir")) + "/mm-modules/mm-lease/src/main/java") // 指定输出目录
                            .commentDate("yyyy-MM-dd");
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.BYTE;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder -> builder
                        .parent("com.mm.cloud.lease")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder ->
                        builder.addInclude("mm_tenant_income,mm_tenant_sub_income") // 设置需要生成的表名
                                .addTablePrefix("mm_") // 设置过滤表前缀
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
